/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formula.one.urjc.circuito.view;

import formula.one.urjc.Metodos;
import formula.one.urjc.circuito.Circuito;
import java.util.ArrayList;

/**
 *
 * @author santiago
 */
public class ModificarCircuito extends javax.swing.JInternalFrame {

    //VARIABLES GLOBALES
    //Declaramos este objeto para ejecutar los metodos genericos
    Metodos metodo;
    ArrayList<Object> arrayCircuitos;
    int longitudArray=0;
    String nombreFichero = "circuitos.dat" ;    

    /**
     * Creates new form ModificarCircuito
     */
    public ModificarCircuito() {
        initComponents();
        metodo = new Metodos();
        arrayCircuitos= metodo.getCircuitoList();
        longitudArray = arrayCircuitos.size();
        cargarCircuito();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        circuitos = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        nombreCircuito = new javax.swing.JTextField();
        aforoCircuito = new javax.swing.JTextField();
        canonCircuito = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        aceptarModificacion = new javax.swing.JButton();
        modificarCancelacion = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextRectas = new javax.swing.JTextField();
        jTextCurvas = new javax.swing.JTextField();

        jLabel1.setText("Modificar Circuito");

        jLabel2.setText("Circuito:");

        circuitos.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                circuitosItemStateChanged(evt);
            }
        });

        jLabel3.setText("Nombre:");

        nombreCircuito.setEnabled(false);
        nombreCircuito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nombreCircuitoActionPerformed(evt);
            }
        });

        aforoCircuito.setEnabled(false);
        aforoCircuito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aforoCircuitoActionPerformed(evt);
            }
        });

        canonCircuito.setEnabled(false);

        jLabel4.setText("Aforo:");

        jLabel5.setText("Canon:");

        aceptarModificacion.setText("Aceptar");
        aceptarModificacion.setEnabled(false);
        aceptarModificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarModificacionActionPerformed(evt);
            }
        });

        modificarCancelacion.setText("Cancelar");
        modificarCancelacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarCancelacionActionPerformed(evt);
            }
        });

        jLabel6.setText("Rectas:");

        jLabel7.setText("Curvas:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(146, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(128, 128, 128))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 51, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nombreCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(circuitos, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(canonCircuito, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                        .addComponent(aforoCircuito, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(jTextCurvas, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextRectas, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(69, 69, 69))
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(aceptarModificacion)
                .addGap(67, 67, 67)
                .addComponent(modificarCancelacion)
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel1)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(circuitos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nombreCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aforoCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(canonCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextRectas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextCurvas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aceptarModificacion)
                    .addComponent(modificarCancelacion))
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nombreCircuitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreCircuitoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nombreCircuitoActionPerformed

    private void aforoCircuitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aforoCircuitoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_aforoCircuitoActionPerformed

    private void modificarCancelacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarCancelacionActionPerformed
        // TODO add your handling code here:
        dispose() ;
    }//GEN-LAST:event_modificarCancelacionActionPerformed

    private void circuitosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_circuitosItemStateChanged
        // TODO add your handling code here:
        // Obtenemos el Nombre del Circuito seleccionado
        String buscarCircuito = (String) circuitos.getSelectedItem() ;
        // Recuperamos los valoras para cada Circuito
        boolean encontrado = false ;
        for (int j = 0; j < longitudArray; j++) {
            Circuito circuitoLista = (Circuito) arrayCircuitos.get(j) ;
            String nombre_Circuito = circuitoLista.getNombre() ;
            encontrado = nombre_Circuito.equals(buscarCircuito) ;
            if (encontrado) {
                System.out.println("Encontrado") ;
                nombreCircuito.setText(nombre_Circuito) ;
                aforoCircuito.setText(Integer.toString(circuitoLista.getAforo())) ;
                aforoCircuito.setEnabled(true) ;
                canonCircuito.setText(Integer.toString(circuitoLista.getCanon())) ;
                canonCircuito.setEnabled(true) ;
                jTextCurvas.setText(circuitoLista.parseString(circuitoLista.getCurvas()));
                jTextRectas.setText(circuitoLista.parseString(circuitoLista.getRectas()));
                /*rectasCircuito.setValue(new Integer(circuitoLista.getRectas().size())) ;
                rectasCircuito.setEnabled(true) ;
                curvasCircuito.setValue(new Integer(circuitoLista.getCurvas().size())) ;
                curvasCircuito.setEnabled(true) ;*/
                aceptarModificacion.setEnabled(true) ;
            }
        }
    }//GEN-LAST:event_circuitosItemStateChanged

    private void aceptarModificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarModificacionActionPerformed
        // TODO add your handling code here:
        // Declaramos las variables con los valores del formulario
        String nombre_Circuito = nombreCircuito.getText() ;
        int aforo_Circuito = Integer.parseInt(aforoCircuito.getText()) ;
        int canon_Circuito = Integer.parseInt(canonCircuito.getText()) ;
        //int rectas_Circuito = (Integer) rectasCircuito.getValue() ;
        //int curvas_Circuito = (Integer) curvasCircuito.getValue() ;
        String rectas_Circuito = jTextRectas.getText() ;
        String curvas_Circuito = jTextCurvas.getText() ;
        Circuito modificar_Circuito = new Circuito(nombre_Circuito, aforo_Circuito,
                                                   canon_Circuito, rectas_Circuito,
                                                   curvas_Circuito) ;
        System.out.println(modificar_Circuito.toString()) ;
        boolean encontrado = false ;
        for (int j=0; j<longitudArray; j++) {
            Circuito circuitoLista = (Circuito) arrayCircuitos.get(j) ;
            String nombre_Circuito_Lista = circuitoLista.getNombre() ;
            encontrado = nombre_Circuito_Lista.equals(nombre_Circuito) ;
            if (encontrado) {
                System.out.println("Encontrado Modificación");
                arrayCircuitos.remove(j);
                arrayCircuitos.add(j,modificar_Circuito) ;
                metodo.borrarFichero(nombreFichero) ;
                metodo.escribirFichero(nombreFichero, arrayCircuitos);
            }
        }        
    }//GEN-LAST:event_aceptarModificacionActionPerformed

    private void cargarCircuito() {
        System.out.println("REGISTROS EN EL FICHERO CARGAR COMBOS");
        circuitos.addItem("");
        for (int i = 0; i < longitudArray; i++) {
            System.out.println("Registro: " + arrayCircuitos.get(i));
            Circuito circuitoCombo = (Circuito) arrayCircuitos.get(i);
            circuitos.addItem(circuitoCombo.getNombre());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarModificacion;
    private javax.swing.JTextField aforoCircuito;
    private javax.swing.JTextField canonCircuito;
    private javax.swing.JComboBox circuitos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextCurvas;
    private javax.swing.JTextField jTextRectas;
    private javax.swing.JButton modificarCancelacion;
    private javax.swing.JTextField nombreCircuito;
    // End of variables declaration//GEN-END:variables
}
